<?php


namespace Admin\Controller;
/**
 * 套餐控制器
 * Class ProductController
 * @package Admin\Controller
 */
class CardController extends AdminController {

    /**套餐列表
     * @author leiying
     */
    public function cardList(){
       
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件
        $searchdata_goods = session('searchdata_card');
        if(IS_POST){
            session('searchdata_card',$get);
        }else{
            $get_p = I('get.p');
            if($get_p){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_card',null);
            }
            $get = $search_data = session('searchdata_card');
        }
        if(isset($get['card_name']) && $get['card_name'] != ''){
            $search_data['card_name'] = $get['card_name'];
            $where['a.card_name'] = array('like',"%".$get['card_name']."%");
        }
        if(isset($get['cate_id']) && $get['cate_id'] != ''){
            $search_data['cate_id'] = $get['cate_id'];
            $where['a.cat_id'] = array('eq',$get['cate_id']);
        }
        if(isset($get['status']) && $get['status'] != ''){
            $search_data['status'] = $get['status'];
            $where['a.status'] = array('eq',$get['status']);
        }

        $count = M('card')->alias('a')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $lists = M('card')
            ->alias('a')
            ->field('a.*')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('a.add_time desc')
            ->select();
       // echo M()->getLastsql();
       // exit;
        $cate=M('card_type')->where('status=1')->select();
        $this->assign('cate',$cate);
        $this->assign('lists',$lists);
        $p = I('get.p',1);
        $this->assign('p',$p);
        $this->assign('searchs',$search_data);
        $this->display();
    }

    public function cardAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
           $data=I('post.');
           $data['market_price']=isset($data['marketPrice'])?floatval($data['marketPrice']):0;
           $data['shop_price']=isset($data['shopPrice'])?floatval($data['shopPrice']):0;
           $data['content']=$data['editorValue'];
           $path = "/Uploads/Card/" . date("Y-m-d") . "/";
            if($data['cover_pic']){
                $url = $this->upload_pic($data['cover_pic'],$path);
                if($url !== false){$data['card_cover'] = $url;}else{$this->error('图片上传失败');}
           }
           $data['add_time']=time();
           $res=M('card')->add($data);
           if($res){
             $this->success('添加成功');
           }else{
             $this->error('添加失败');
           }
        }else{
            $category=M('card_type')->where('status=1')->select();
            $this->assign('category',$category);
            $this->display();
        }
    }

     public function cardEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
           $data=I('post.');
           $id=$data['id'];
           $data['market_price']=isset($data['marketPrice'])?floatval($data['marketPrice']):0;
           $data['shop_price']=isset($data['shopPrice'])?floatval($data['shopPrice']):0;
           $data['content']=$data['editorValue'];
           $row=M('card')->where('id='.$id)->find();
           $path = "/Uploads/Card/". date("Y-m-d") . "/";
            if($data['new_cover_pic']){
                $url = $this->upload_pic($data['new_cover_pic'],$path);
                if($url !== false){
                    $delete_path = $_SERVER['DOCUMENT_ROOT'].$row['card_cover'];
                    @unlink($delete_path);
                    $data['card_cover'] = $url;
                }else{$this->error('图片上传失败');}
            }
           $data['add_time']=time();
           $res=M('card')->where('id='.$id)->save($data);
           if($res){
             $this->success('添加成功');
           }else{
             $this->error('添加失败');
           }
        }else{
            $category=M('card_type')->where('status=1')->select();
            $card=M('card')->where('id='.I('id'))->find();
            $this->assign('card',$card);
            $this->assign('category',$category);
            $this->display();
        }
    }



    /**获取分类的子分类id(包含该分类id)
     * @param $cat_id
     * @return array
     * @author leiying
     */
    protected  function getCategoryId($cat_id){
        $tree_list = M('goods_category')->field("id,pid,name")->order("sort asc")->select();
        $res = list_to_tree($tree_list,"id","pid","_child",$cat_id);
        $lists = tree_to_array($res);
        $ids[] = $cat_id;
        foreach($lists as $v){
            $ids[] = $v['id'];
        }
        return $ids;
    }

      //救护类型
    public function categoryList(){
        $lists=$tree_list = M('card_type')->order("id asc")->select();
        $ztree = json_encode($lists);
        $this->assign('ztree',$ztree);
        $this->display();
    }


    public function categoryAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $data = I('post.');
            $path = "/Uploads/Card/" . date("Y-m-d") . "/";
            if($data['cover_pic']){
                $url = $this->upload_pic($data['cover_pic'],$path);
                if($url !== false){$data['logo'] = $url;}else{$this->error('图片上传失败');}
            }
            //验证通过了
            $res = M('card_type')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
          
          $this->display();  
        }
        
    }
     public function categoryEdit(){
        if(IS_POST){
            $data = I('post.');
            $id = $data['id'];
            //处理参数
            if(empty($id)){
              $this->error('请选择需要操作的数据');
            }
            
            $row = M('Card_type')->where(array('id'=>$id))->find();
            $path = "/Uploads/Card/". date("Y-m-d") . "/";
            if($data['new_cover_pic']){
                $url = $this->upload_pic($data['new_cover_pic'],$path);
                if($url !== false){
                    $delete_path = $_SERVER['DOCUMENT_ROOT'].$row['logo'];
                    @unlink($delete_path);
                    $data['logo'] = $url;
                }else{$this->error('图片上传失败');}
            }
            $res = M('card_type')->where(array('id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{

          $data=M('Card_type')->where('id='.I('id'))->find();
          $this->assign('data',$data);
          $this->display();  
        }
        
    }

    //删除类型
    public function delCardType(){
        $id=I('post.id',0,'intval');
        $res = M('card_type')->where(array('id'=>$id))->delete();
        if($res){
           $this->success('删除成功');
        }else{
           $this->error('删除失败');
        }
    }
  
}
 